package com.billion.message.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.billion.common.utils.StringUtils;
import com.billion.msg.entity.Token;
import com.billion.msg.service.IMessageService;
import com.billion.msg.utils.MessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author 王琦
 * @date 2022/1/14
 */
@Slf4j
@Service
public class MessageServiceImpl implements IMessageService {

    /**
     * 发送微信消息
     * @param appid
     * @param appSecret
     * @param jsonObject
     */
    @Override
    public void sendTemplateMessage(String appid, String appSecret, JSONObject jsonObject){
        Token token = MessageUtil.getAccessToken(appid, appSecret);
        String accessToken = token.getAccessToken();
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;

        JSONObject jsonResult = MessageUtil.httpsRequest(url, "POST", jsonObject.toJSONString());
        if(StringUtils.isNotNull(jsonResult)){
            int errCode = jsonResult.getInteger("errcode");
            String errMsg = jsonResult.getString("errmsg");
            if(errCode != 0){
                log.error("消息发送失败:" + errCode + "," + errMsg);
                log.error("消息内容：" + jsonObject.toJSONString());
            }
        }
    }

    public static void main(String[] args) {
        String appid = "wxe75898d3dd45c70d";
        String appSecret = "df8d31591c10e26036ac15905a996161";
        String openId = "oa0Bn5ZeJ-CDl6CU3V6YJc94PRpE";

        Token token = MessageUtil.getAccessToken(appid, appSecret);
        String accessToken = token.getAccessToken();
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("touser", openId);
        jsonObject.put("template_id", "hJp7TQ8QoRt0Kem-jYdONC5023iL6W_0-yKljI4ZB50");
        jsonObject.put("page", "pages/main/meter/index?meterId=" + 1);

        JSONObject dataJson = new JSONObject();
        JSONObject amount1 = new JSONObject();
        amount1.put("value", "-23");
        dataJson.put("amount1", amount1);
        JSONObject thing3 = new JSONObject();
        thing3.put("value", "502");
        dataJson.put("thing3", thing3);
        JSONObject number13 = new JSONObject();
        number13.put("value", "202206020105");
        dataJson.put("number13", number13);
        JSONObject thing4 = new JSONObject();
        thing4.put("value", "当前余额已不足20元，请续交费用");
        dataJson.put("thing4", thing4);
        jsonObject.put("data", dataJson);

        JSONObject jsonResult = MessageUtil.httpsRequest(url, "POST", jsonObject.toJSONString());
        if(StringUtils.isNotNull(jsonResult)){
            int errCode = jsonResult.getInteger("errcode");
            String errMsg = jsonResult.getString("errmsg");
            if(errCode != 0){
                log.error("消息发送失败:" + errCode + "," + errMsg);
            }
        }
    }
}
